<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        inline size="mini"
        :model="query"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-form-item label="性别:">
          <el-select
              style="width: 100%"
              clearable
              v-model="query.sex"
              placeholder="请选择性别"
          >
            <el-option value="0" label="女" />
            <el-option value="1" label="男" />
            <el-option value="2" label="保密" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
              placeholder="请输入姓名或账户名称，联系方式"
              v-model="query.keyword"
              style="width: 100%"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload"
          >查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="toolbar-box" style="margin-bottom: 4px">
        <div class="tool-btn">
          <!-- <el-button
            size="small"
            type="default"
            icon="el-icon-upload"
            @click="importTable"
            >批量导入</el-button
          > -->
          <el-button
              size="mini"
              type="default"
              icon="el-icon-download"
              @click="exportTable"
          >导出</el-button
          >
        </div>
        <div class="tool-tab">
          <!-- <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            >新增</el-button
          > -->
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            :disabled="!selection.length"
            @click="removeBatch"
            >删除
          </el-button>
        </div>
      </div>
      <!-- 数据表格 -->
      <el-table
        border
        lazy
        ref="table"
        :data="data"
        v-loading="loading"
        height="calc(100vh - 410px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column label="序号" width="55" align="center" type="index">
          <template slot-scope="scope">
            <span
              v-text="$setTableIndex(query.page, query.limit, scope.$index)"
            >
            </span>
          </template>
        </el-table-column>
        <el-table-column label="头像" align="center" prop="avatar">
          <template slot-scope="scope">
            <img :src="getimageUrl(scope.row.avatar)" style="width: 50px; height: 50px">
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center" prop="realname">
        </el-table-column>
        <el-table-column label="账户名称" align="center" prop="username" />
        <el-table-column label="角色" align="center" prop="role_name" />
        <el-table-column label="联系方式" align="center" prop="mobile" />
        <el-table-column label="绑定设备IMEI" align="center" prop="imei" />
        <el-table-column label="性别" align="center" prop="sex">
          <template slot-scope="scope">
            {{ scope.row.sex == 0 ? "女" : scope.row.sex == 1 ? "男" : "保密" }}
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="create_time"
          width="160"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          :resizable="false"
          width="200"
        >
          <template slot-scope="{ row }">
            <el-link
              type="text"
              :underline="false"
              icon="el-icon-edit"
              @click="handleEdit(row)"
              >修改
            </el-link>
            <el-popconfirm
              class="ele-action"
              title="确定要删除该用户吗？"
              @confirm="remove(row)"
            >
              <el-link
                type="text"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                >删除
              </el-link>
            </el-popconfirm>
            <el-link
              type="text"
              :underline="false"
              icon="el-icon-edit"
              @click="
                obj_xgMima.id = row.id;
                obj_xgMima.username = row.username;
                obj_xgMimaShow = true;
              "
              >修改密码
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getData"
      />
    </el-card>
    <el-dialog title="修改密码" :visible.sync="obj_xgMimaShow" width="500px">
      <el-form :model="obj_xgMima" ref="obj_xgMima">
        <el-form-item label="账号" label-width="150px" prop="username">
          <el-input
            clearable
            :disabled="true"
            :maxlength="20"
            v-model="obj_xgMima.username"
            placeholder="请输入名称"
          />
        </el-form-item>
        <el-form-item label="密码" label-width="150px" prop="password" :rules="[{
required: this.obj_xgMima.id, message: '请输入修改密码', trigger: 'blur'}
        ,{ pattern: /^(?=.*[A-Za-z])[\w]{6,12}$/, message: '登录密码格式不正确,6-12位数字加字母', trigger: 'blur'}]">
          <el-input
            clearable
            :maxlength="20"
            type="password"
            v-model="obj_xgMima.password"
            placeholder="请输入新密码"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="obj_xgMimaShow = false">取 消</el-button>
        <el-button type="primary" @click="obj_xgMima_fn">确定修改</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      width="1000px"
      append-to-body
      @closed="cancel"
      :visible.sync="showEdit"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="IMEI">
              <el-input v-model="form.imei" placeholder="请输入设备IMEI" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务IP">
              <el-input v-model="form.ip" placeholder="请输入设备服务IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="RFID">
              <el-input v-model="form.rfid" placeholder="请输入设备RFID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号">
              <el-input v-model="form.ver" placeholder="请输入设备版本号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号">
              <el-select v-model="form.model_id" style="width: 100%">
                <el-option
                  :key="i"
                  :value="item.id"
                  :label="item.name"
                  v-for="(item, i) in modelList"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="蓝牙MAC">
              <el-input v-model="form.mac" placeholder="请输入设备蓝牙MAC" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="批量导入"
      width="500px"
      append-to-body
      @closed="cancelImport"
      :visible.sync="showImport"
      :close-on-click-modal="false"
    >
      <dl class="uploadlist">
        <dt>上传文件：</dt>
        <dd>
          <el-upload
            :multiple="false"
            action=""
            :on-change="handleFileChange"
            :on-remove="handleFileRemove"
            :auto-upload="false"
            :file-list="fileList"
            :limit="1"
            :on-exceed="handleExceed"
            :show-file-list="true"
            class="upload-file-uploader"
            ref="fileUpload"
          >
            <!-- 上传按钮 -->
            <el-button type="primary">选择文件</el-button>
          </el-upload>
        </dd>
      </dl>
      <dl class="uploadlist">
        <dt>模板：</dt>
        <dd>
          <a
            href="https://api.gdjx-ai.com/uploads/imei.xlsx"
            target="_blank"
            >下载模板</a
          >
        </dd>
      </dl>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitImportFile">确 定</el-button>
        <el-button @click="cancelImport">取 消</el-button>
      </span>
    </el-dialog>
     <!-- 编辑弹窗 -->
     <indexList-edit
     :data="current"
     :visible.sync="showEdit11"
     @done="reload"
   />
  </div>
</template>

<script>
import indexListEdit from "./indexList-edit.vue";
import { mapGetters } from "vuex";

export default {
  name: "DeviceIMEI",
  components: { indexListEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      imageUrl:'https://api.gdjx-ai.com',
      loading: false, // 表格加载状态
      ids: [], // 选中数组
      selection: [], // 表格选中数据
      total: 0, // 数据总数
      data: [], // 表格数据接口
      modelList: [],
      // 表格搜索条件
      query: {
        page: 1,
        limit: 10,
      },
      obj_xgMimaShow: false,
      obj_xgMima: {
        id: "",
        password: "",
        username: "",
      },
      model_query: {
        page: 1,
        limit: 99999,
      },
      current:null,
      showEdit11:false,
      showEdit: false, // 是否显示编辑弹窗
      showImport: false, // 显示导入弹窗
      fileList: [], // 上传发票文件列表
      title: "",
      form: {}, //  新增编辑表单// 表单校验
      rules: {
        modelName: [
          { required: true, message: "型号名称不能为空", trigger: "blur" },
        ],
      }
    };
  },
  mounted() {
    this.getData();
    this.getModelList();
  },
  methods: {
    getimageUrl(img) { 
      if(img) {
        if (img.includes(this.imageUrl)) {
          return img
         } else { 
          return this.imageUrl + img
        }
    
      }else{ 
        return ''
      }
    },
    obj_xgMima_fn() {
      this.$refs['obj_xgMima'].validate((valid) => {
        if (valid) {
          let idRule = /^(?=.*[A-Za-z])[\w]{6,12}$/.test(this.obj_xgMima.password)
          if (this.obj_xgMima.password && !idRule) {
            return false
          }
          this.$http
            .post("/student_parent/edit", {
              ...this.obj_xgMima,
            })
            .then(() => {
              this.$message.success("修改成功");
              this.obj_xgMimaShow = false;
            })
            .catch(() => {
              this.obj_xgMimaShow = false;
            });
        } else {
            return false;
        }
      });
    },
    /* 获取表格数据 */
    getData() {
      this.loading = true;
      this.$http
        .get("/student_parent/index", { params: this.query })
        .then((res) => {
          this.loading = false;
          this.data = res.data.data;
          this.total = res.data.count;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    getModelList() {
      this.$http
        .get("/device_model/index", { params: this.model_query })
        .then((res) => {
          this.modelList = res.data.data;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 查询数据 */
    reload() {
      this.getData();
    },
    /* 重置搜索 */
    reset() {
      this.query = {
        page: 1,
        limit: 10,
      };
      this.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selection = selection;
    },
    /* 删除 */
    remove(row) {
      this.loading = true;
      const params = { id: row.id };
      this.$http
        .get("/student_parent/delete", { params: params })
        .then((res) => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.getData();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的用户吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          this.$http
            .post("/student_parent/delete", {
              id: this.selection.map((d) => d.id),
            })
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.getData();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 新增数据 */
    handleAdd() {
      this.showEdit = true;
      this.title = "新增设备IMEI";
      this.form = {};
    },
    /* 修改数据 */
    handleEdit(row) {
      this.current = row;
      if (this.current) { 
        this.current.avatar=this.getimageUrl(row.avatar)
      }
      this.showEdit11 = true;
    },
    /* 取消新增修改 */
    cancel() {
      this.showEdit = false;
      this.form = {};
    },
    /* 提交表单 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$http
            .post("/device_imei/edit", this.form)
            .then((res) => {
              console.log(res);
              this.$message.success("操作成功");
              this.cancel();
              this.getData();
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        }
      });
    },
    submitImportFile(){},
    /* 导入数据 */
    importTable() {
      this.showImport = true;
    },
    /* 取消导入 */
    cancelImport() {
      this.showImport = false;
    },
    // 上传组件上传的文件变更
    handleFileChange(file, fileList) {
      this.fileList = [];
      this.fileList = fileList;
    },
    // 删除上传的文件
    handleFileRemove(file, fileList) {
      this.fileList = [];
      this.fileList = fileList;
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`只能上传1个文件!`);
    },
    /* 导出数据 */
    exportTable() {
      this.$http
        .get("/student_parent/exportExcel", {
          params: { ...this.query, export_type: 0 },
        })
        .then((res) => {
          let data = res.data;
          if (data.code == 0) {
            // 下载文件
            window.location.href = data.data;
            this.$message({
              type: "success",
              message: "导出成功",
            });
          } else {
            this.$message.error("导出失败");
          }
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-dialog:not(.ele-dialog-form)
  .el-dialog__body
  .el-form
  .el-form-item:last-child {
  margin-bottom: 22px;
}
.toolbar-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .tool-tab {
    height: 32px;
    ul {
      height: 100%;
      display: flex;
      list-style: none;
      align-items: center;
      li {
        height: 100%;
        padding: 0 10px;
        border-right: 2px solid #ddd;
        &:first-child {
          padding-left: 0;
        }
        &:last-child {
          padding-right: 0;
          border-right: 0;
        }
        span {
          display: block;
          font-size: 14px;
          cursor: pointer;
          padding: 7px 10px;
          border-radius: 4px;
          border: 1px solid #ddd;
          &.active {
            color: #ffffff;
            border-color: #0f40f5;
            background: #0f40f5;
          }
        }
      }
    }
  }
}

.uploadlist {
  margin: 0;
  display: flex;
  margin-bottom: 15px;
  justify-content: flex-start;
  dt {
    height: 40px;
    font-size: 16px;
    line-height: 40px;
  }
  dd {
    font-size: 16px;
    width: calc(100% - 130px);
    margin: 0;
    line-height: 40px;
  }
}
</style>
